import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/*@Use: This program will be used to calculate document frquecny of the words
 * 
 * 
 * */
public class docFreq 
{
	
	String username = "";
	String password = "";
	String database = "";
	String table = "";
	String term1 = "";
	String term2="";
	Connection conn;
	
	//take inout
	
	private void input()
	{
	try 
		{
		BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Enter DB user (Default root ) ");
		username = br.readLine();
		System.out.println("Enter DB Password");
		password = br.readLine();
		
		System.out.println("Enter database");
		database = br.readLine();
		

		System.out.println("Enter table");
		table = br.readLine();
		
		System.out.println("Enter Term1=");
		term1 = br.readLine();
		
		System.out.println("Enter term2");
		term2 = br.readLine();
		// String username = "root";//"Sangeeta";//"root";
		// String paswd = "123";//"sangeeta@123456";//"123";
		
		} catch (IOException e) 
		{ 	e.printStackTrace(); }
		
		
	}
	
	
	private void calDocFreq(Connection conn2, String term1, String term2) 
	{

     String both_str  = "select count(*) from  "+ table +  " where  (title like \"% "+ term1+" %\" or description" +
     		" like \"% "+term1+" %\") and (title like \"% "+ term2+" %\" or description" +
     		" like \"% "+term2+" %\") ";
     System.out.println(" No of documents in which both co-occur="+both_str);
     
     String term1_count  = "select count(*) from  "+ table +  " where  (title like \"% "+ term1+" %\" or description" +
		" like \"% "+term1+" %\") ";
     String term2_count  = "select count(*) from  "+ table +  " where  (title like \"% "+ term2+" %\" or description" +
		" like \"% "+term2+" %\")";
     
     try 
     {
		Statement stmt = conn2.createStatement();
		
		stmt.execute(both_str);
		ResultSet rs =  stmt.getResultSet();
		
		int count_both=0;
		int count_term1 =0;
		int count_term2=0;
		while(rs.next())
		{
			count_both = rs.getInt("count(*)");
		}
	
		stmt.execute(term1_count);
		rs =  stmt.getResultSet();
		while(rs.next())
		{
			count_term1 = rs.getInt("count(*)");
		}
		
        stmt.execute(term2_count);
        rs =  stmt.getResultSet();
		
		while(rs.next())
		{
			count_term2 = rs.getInt("count(*)");
		}
     
		System.out.println("Both count="+count_both+" \n count term1= "+count_term1+" \n count term2="+
				count_term2);
		
		if(stmt!=null)
			stmt.close();
     } catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
     
     
     
	}
     
	public static void main(String srgs[])
	{
		docFreq  df =  new docFreq();
		df.input();
		db_conn db =  new db_conn();
		df.conn = db.initdb(df.database, df.table,df.username, df.password);
		df.calDocFreq(df.conn, df.term1, df.term2);
		
		
	}

	
}
